package com.atguigu.gulimall.order.domain.vo;

import com.atguigu.gulimall.order.domain.response.CartItemResp;
import com.atguigu.gulimall.order.domain.response.ReceiveAddressResp;
import lombok.*;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/1 21:30
 */
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class OrderConfirmVO implements Serializable {

    private static final long serialVersionUID = 4215197505043761384L;

    /**
     * 收货地址
     */
    private List<ReceiveAddressResp> addresses;

    /**
     * 购物项
     */
    private List<CartItemResp> cartItems;

    /**
     * 积分
     */
    private Integer integration;

    /**
     * 总价格
     */
    private BigDecimal totalPrice;

    /**
     * 应付价格
     */
    private BigDecimal pricePayable;

    /**
     * 商品数量
     */
    private Integer count;
    /**
     * 订单令牌
     */
    private String orderToken;
    
    private Map<Long, Boolean> stocks = new HashMap<>();

    /**
     * 总价格
     */
    public BigDecimal getTotalPrice() {
        BigDecimal totalPrice = new BigDecimal("0.0");
        if (cartItems != null && cartItems.size() > 0) {
            for (CartItemResp cartItem : cartItems) {
                //商品单价
                BigDecimal price = cartItem.getPrice();
                //商品数量
                BigDecimal count = new BigDecimal(String.valueOf(cartItem.getCount()));
                BigDecimal priceSum = price.multiply(count);
                totalPrice = totalPrice.add(priceSum);
            }
        }
        this.totalPrice = totalPrice;
        return totalPrice;
    }
    

    /**
     * 应付价格
     */
    public BigDecimal getPricePayable() {
        BigDecimal totalPrice = getTotalPrice();
        if (integration != null) {
            BigDecimal integrationDecimal = new BigDecimal(String.valueOf(integration));
            totalPrice = totalPrice.subtract(integrationDecimal);
        }
        this.pricePayable = totalPrice;
        return totalPrice;
    }

    public Integer getCount() {
        Integer count = 0;
        if (cartItems != null && cartItems.size() > 0) {
            for (CartItemResp cartItem : cartItems) {
                count += cartItem.getCount();
            }
        }
        this.count = count;
        return count;
    }
}
